package com.caiheng.api.service.estate.impl;

import com.caiheng.api.dao.estate.TerminalsMapper;
import com.caiheng.api.dao.estate.TerminalCheckRecordImgMapper;
import com.caiheng.api.dao.estate.TerminalCheckRecordMapper;
import com.caiheng.api.dao.sys.SysUserMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.estate.Terminals;
import com.caiheng.api.entity.estate.TerminalCheckRecord;
import com.caiheng.api.entity.estate.TerminalCheckRecordImg;
import com.caiheng.api.entity.sys.SysUser;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.estate.TerminalCheckRecordService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
public class TerminalCheckRecordServiceImpl extends BaseServiceImpl<TerminalCheckRecordMapper, TerminalCheckRecord>
        implements TerminalCheckRecordService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private TerminalsMapper terminalsMapper;
    @Resource
    private TerminalCheckRecordMapper terminalCheckRecordMapper;
    @Resource
    private TerminalCheckRecordImgMapper terminalCheckRecordImgMapper;

    @Value("${web.imgPrefix}")
    private String imgPrefix;//图片地址
    @Override
    public List<TerminalCheckRecord> getListBySysUserId(User user, Integer is_clean, Integer check_state,
                                                        Integer offset, Integer pageSize) {
        Map<String,Object> map  = new HashMap<>();
        SysUser sysUser = sysUserMapper.selectById(user.getSysUserId());

        if(sysUser != null){
//            if(sysUser.getUserType() >= 4){
//                map.put("shop_id",sysUser.getShopId());
//            } else if(sysUser.getUserType() == 2){
//                map.put("jituan_user_id",sysUser.getShopId());
//            } else if(sysUser.getUserType() == 3){
//                map.put("sys_user_id",sysUser.getShopId());
//            }
        }
        if(check_state == -1){//查询全部的
//            map.put("sys_user_id",sysUser.getUserId());
            map.put("is_clean",is_clean);
        }else{//指定 是否是 报修
            map.put("check_state",check_state);
        }
        List<TerminalCheckRecord> list = new ArrayList<>();
        list = terminalCheckRecordMapper.getPageByUserId(map,offset,pageSize);
        list.forEach(record -> {
            User user1 = userMapper.selectById(record.getWxUserId());
            if(user1!=null){
                SysUser sysUser1 = sysUserMapper.selectById(user1.getSysUserId());
                if(sysUser1!=null){
                    record.setSysUserName(sysUser1.getNickName());
                }
            }
            List<TerminalCheckRecordImg> imgs = terminalCheckRecordImgMapper.selectListByColumn("record_id",record.getId());
            if(imgs!=null){
                imgs.forEach(img->{
                    img.setImgUrl(imgPrefix +img.getImgUrl());
                });
            }
            record.setImgs(imgs);
        });
        return list;
    }

    @Override
    @Transactional
    public int addRecord(TerminalCheckRecord record, Terminals terminal) throws TransactionalException {
        try{
            int ret = terminalCheckRecordMapper.insert(record);
            if(ret <= 0){
                throw new TransactionalException("ckTerminalCheckRecordMapper insert error");
            }
            return ret;
        }catch (Exception ex){
            ex.printStackTrace();
            throw new TransactionalException("addRecord system error");
        }
    }
}
